package com.darker.secking.validator;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.darker.secking.utils.ValidatorUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required=false;


    //初始化
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();

    }

    @Override
    public boolean isValid(String mobile, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            //如果填写了手机号码就进行校验
            return ValidatorUtil.isMobile(mobile);
        }else
        {
            if(StringUtils.isEmpty(mobile)){
                return true;
            }
            else {
                return ValidatorUtil.isMobile(mobile);
            }

        }

    }
}
